/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
 // 递归
func hasPathSum(root *TreeNode, targetSum int) bool {
    var res bool
    if root == nil {
        return res
    }
    pathSum(root, 0, targetSum, &res)
    return res
}

func pathSum(root *TreeNode, sum int, targetSum int, res *bool){
    sum += root.Val
    if root.Left == nil && root.Right == nil && sum == targetSum {
        *res = true
    }
    if root.Left != nil && !*res {
        pathSum(root.Left, sum, targetSum, res)
    }
    if root.Right != nil && !*res {
        pathSum(root.Right, sum, targetSum, res)
    }
}